//
// Created by dong on 2025/10/20.
//

#ifndef BASEC_SOBJ_H
#define BASEC_SOBJ_H
#include "demo101.h"
/***
 * 管理所有对象
 *
 *
 */

class Sobj;

using SobjList = vector<Sobj*>;


class Sobj
{
public:
    Sobj(Sobj* parent=nullptr);
    virtual ~Sobj();
    void setObjName(string& objName);
    virtual bool event(SDL_Event* msg);
    bool isRelease();
protected:
    Sobj* m_parent; //父对象
    SobjList m_children; //子对象
    string m_objname; //对象名
    bool m_autoRelease; //控制对象的存在或者释放
};

inline Sobj::Sobj(Sobj* parent)
{
    if (parent!=nullptr)
    {
        m_parent->m_children.push_back(this);
    }
}

inline Sobj::~Sobj()
{
    if (m_parent)
    {
        auto& children = m_parent->m_children;
        auto iter = find(children.begin(),children.end(),this);
        if (iter!=children.end())
        {
            m_parent->m_children.erase(iter);
        }
        for (auto object: this->m_children)
        {
            if (object->isRelease())
            {
                delete  object;
            }
            m_children.clear();
        }
    }
}

inline void Sobj::setObjName(string& objName)
{
    m_objname = objName;
}

inline bool Sobj::event(SDL_Event* msg)
{
    // SDL_Log("SObject::event");
    return false;
}

inline bool Sobj::isRelease()
{
    return m_autoRelease;
}


#endif //BASEC_SOBJ_H
